<template>
    <div>
        <div>
            <input type="text" v-model="form.title" />
        </div>
        <div class="mt-3">
            <textarea type="text" v-model="form.content" />
        </div>
        <div>
            <button class="border py-3 px-2 " @click="submit">保存提交</button>
        </div>
    </div>
</template>

<script setup>
import { defineProps, ref, reactive } from 'vue'
import axios from '@/plugins/axios'
import { useRouter } from 'vue-router'
const router = useRouter()

const props = defineProps({
    news: { type: Object, default: () => ({ title: '', content: '' }) }
})

const form = reactive(props.news)

const submit = async () => {
    const action = form.id ? 'put' : 'post'
    const url = form.id ? `news/${form.id}` : `news`
    await axios[action](url, form)
    router.push({ name: 'home' })
}
</script>

<style></style>
